//
//  OtherChargeList.m
//  HondaPro_2
//
//  Created by Eric on 13-6-3.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "OtherChargeList.h"
#import "MainMenuDataModel.h"
#import "UserDefinedCellForSubView3ForGetJobSheet.h"

@interface OtherChargeList ()

/** 收费区分列表 */
@property(nonatomic,strong)NSMutableDictionary *chargeDiff;

//创建控件
-(void)createStaticImg;
-(void)createTableView;
-(void)createLbl;

//刷新控件内容
-(void)freshenViewDis;

@end

@implementation OtherChargeList

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [self createStaticImg];
        [self createTableView];
        [self createLbl];
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    [self freshenViewDis];
}

#pragma mark - 创建控件

-(void)createStaticImg
{
    NSArray *arrForAllKeys = [NSArray arrayWithObjects:
                              @"CHARGE_PARTITION_CODE",
                              @"ADD_ITEM_CODE",
                              @"ADD_ITEM_NAME",
                              @"ADD_ITEM_AMOUNT",
                              @"DISCOUNT",nil];
    
    CGFloat lblY = 0;
    CGFloat lblH = 50;
    CGFloat lblW = self.frame.size.width / 5;
    NSDictionary *dicForStaticLabelsText = [NSDictionary dictionaryWithObjectsAndKeys:
                                            @"收费区分",@"CHARGE_PARTITION_CODE",
                                            @"附加项目代码",@"ADD_ITEM_CODE",
                                            @"附加项目名称",@"ADD_ITEM_NAME",
                                            @"金额",@"ADD_ITEM_AMOUNT",@"折扣",@"DISCOUNT",nil];
    
    for (int i = 0; i<[arrForAllKeys count]; i++)
    {
        NSString *strKeys = [arrForAllKeys objectAtIndex:i];
        CGRect rect = CGRectMake(i * lblW, lblY, lblW, lblH);
        UILabel *lbl = [[UILabel alloc] init];
        lbl.frame = rect;
        lbl.textColor = [UIColor grayColor];
        lbl.backgroundColor = [UIColor groupTableViewBackgroundColor];
        lbl.font = [UIFont fontWithName:@"ArialMT" size:16];
        lbl.text = [dicForStaticLabelsText objectForKey:strKeys];
        [self addSubview:lbl];
    }
}

-(void)createTableView
{
    tableViewDis = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.frame.size.width, 260)];
    tableViewDis.backgroundColor = [UIColor clearColor];
    
    tableViewDis.dataSource = self;
    tableViewDis.delegate = self;
    
    [self addSubview:tableViewDis];
}

-(void)createLbl
{
    otherChargeTotal = [[UILabel alloc] initWithFrame:CGRectMake(600, 400, 300, 50)];
    otherChargeTotal.backgroundColor = [UIColor whiteColor];
    otherChargeTotal.font = [UIFont systemFontOfSize:15];
    otherChargeTotal.textColor = [UIColor darkGrayColor];
    [self addSubview:otherChargeTotal];
}

- (NSString *)transString:(id)obj
{
    if ([obj isKindOfClass:[NSString class]]) {
        return obj;
    }
    
    NSNumber *newObj = (NSNumber *)obj;
    return newObj.stringValue;
}

- (NSMutableDictionary *)readershuDICimform
{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *str=[path stringByAppendingPathComponent:@"Dic.text"];
    dic=[NSMutableDictionary dictionaryWithContentsOfFile:str];
    //nslog(@"%@",dic);
    return dic;
}

#pragma mark - 刷新控件内容
-(void)freshenViewDis
{
    [tableViewDis reloadData];
    
    double total = 0;
    for (NSDictionary *dic in self.otherChargeDic) {
        total += [dic[@"ADD_ITEM_AMOUNT"]doubleValue];
    }
    otherChargeTotal.text = [NSString stringWithFormat:@"其它费用合计：%.2f",total];
}

#pragma mark - tableView DataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6+self.otherChargeDic.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UserDefinedCellForSubView3ForGetJobSheet *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (nil == cell)
    {
        cell = [[UserDefinedCellForSubView3ForGetJobSheet alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    //cell的风格是：浅浅灰色和浅灰色间隔出现，第一行是浅灰色
    NSInteger row = [indexPath row];
    switch (row%2)
    {
        case 0:
            //浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
            break;
        case 1:
            //浅浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
            break;
            
        default:
            break;
    }
    
    NSArray *arrRepairItemList = self.otherChargeDic;
    if (row<[arrRepairItemList count])
    {
        NSDictionary *dicRepairItemList = [arrRepairItemList objectAtIndex:row];
        for (int i = 0; i<[cell.arrForAllLabelsKeysOnCell count]; i++)
        {
            NSString *strKey =cell.arrForAllLabelsKeysOnCell[i];
            id text = dicRepairItemList[strKey];
            if ([text isKindOfClass:[NSNumber class]]) {//如果是数字类型
                text = [text stringValue];
            }
            else if ([strKey isEqualToString:@"CHARGE_PARTITION_CODE"]){
                text = self.chargeDiff[dicRepairItemList[strKey]];
            }
            
            ((UILabel *)cell.dicForAllLabelsOnCell[strKey]).text = text;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    else
    {
        for (int i = 0; i<[cell.arrForAllLabelsKeysOnCell count]; i++)
        {
            ((UILabel *)cell.dicForAllLabelsOnCell[cell.arrForAllLabelsKeysOnCell[i]]).text = @"";
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

#pragma mark - tableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

//创建一个静态lbl
-(UILabel *)createAnStaticLblByFrame:(CGRect)xingCanRect withTxt:(NSString *)strTxt andBackgroundColor:(UIColor *)bgColor
{
    UILabel *lbl = [[UILabel alloc] initWithFrame:xingCanRect];
    lbl.backgroundColor = bgColor;
    lbl.font = [UIFont systemFontOfSize:13];
    lbl.textColor = [UIColor darkGrayColor];
    lbl.text = strTxt;
    
    if ([strTxt isEqualToString:@"序号"])
    {
        lbl.textAlignment = UITextAlignmentCenter;
    }
    
    return lbl;
}

-(NSMutableDictionary *)chargeDiff{
    if (_chargeDiff == nil) {
        _chargeDiff = [NSMutableDictionary dictionary];
        NSMutableArray *arr = [self readershuDICimform][@"TM_CHARGE_PARTITION"];
        for (NSDictionary *dic in arr) {
            NSString *name = dic[@"NAME"];
            NSString *val = dic[@"CODE"];
            [_chargeDiff setObject:name forKey:val];
        }
    }
    return _chargeDiff;
}

@end
